package com.example.springbootdemo.controller;

import com.example.springbootdemo.bean.User;
import com.example.springbootdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;
    @RequestMapping("/ToLogin")
    public String ToLogin() {
        return "login";
    }

    @RequestMapping("/Login")
    public String Login(String username, String password) {
        List<User> userList = userMapper.Login(username,password);
        if (userList == null || userList.isEmpty()){
            return "login";
        }else {
            return "success";
        }
    }

    @RequestMapping("/ToRegister")
    public String ToRegister() {return "register";}

    @RequestMapping("/Register")
    public String Register(User user) {
        List<User> list = userMapper.CheckUserName(user.getUsername());
        if (list.isEmpty()){
            userMapper.Insert(user);
            return "login";
        }else {
            return "register";
        }
    }
}
